package com.cz.nongyetongv5.receiver;

import android.app.DownloadManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import java.io.File;

public class UpdateReceiver extends BroadcastReceiver {

    private File apkFile = null;

    private long downloadId;

    private Context context = null;

    public UpdateReceiver(File apkFile, long downloadId, Context context) {

        this.apkFile = apkFile;

        this.downloadId = downloadId;

        this.context = context;

    }

    @Override
    public void onReceive(Context context, Intent intent) {

        long reference = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,
                -1);

        if (downloadId == reference) {

            if (apkFile.exists()) {

                installApk(apkFile);
            }
        }
    }

    private void installApk(File file) {
        Intent intent = new Intent();
        intent.setAction(android.content.Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file),
                "application/vnd.android.package-archive");
        context.startActivity(intent);

    }
}
